<template>
  <el-dialog
    :title="!this.id ? '新增' : '查看'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="70%">
    <el-button type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="headImg"
        header-align="center"
        align="center"
        label="头像">
        <template slot-scope="scope">
          <img v-if="scope.row.headImg !== ''" :src="'https://image.yilianbaihui.cn/consumer/' + scope.row.headImg" style="width: 80px; heidth: 80px"/>
        </template>
      </el-table-column>
      <el-table-column
        prop="realName"
        header-align="center"
        align="center"
        label="评论人">
      </el-table-column>
      <el-table-column
        prop="commentSrc"
        header-align="center"
        align="center"
        label="评论来源">
        <template slot-scope="scope">
          <el-tag type="info">{{scope.row.commentSrc === 0 ? '送贝' : (scope.row.commentSrc ===1 ? '淘宝' : "送贝")}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="content"
        header-align="center"
        align="center"
        label="评论内容">
      </el-table-column>
      <el-table-column
        prop="commentImages"
        header-align="center"
        align="center"
        label="评论图片">
        <template slot-scope="scope">
          <span v-if="scope.row.commentImages !== '' && scope.row.commentImages !== null">
            <el-popover
            style="margin-left: 5px;margin-bottom: 5px"
            placement="right"
            title=""
            trigger="hover" v-for="(item, index) in scope.row.commentImages.split(',')" :key="index">
                <img v-if="item !== null" :src="'https://image.yilianbaihui.cn/consumer/' +item" style="max-width: 300px"/>
                <img slot="reference" v-if="item !== null" :src="'https://image.yilianbaihui.cn/consumer/' + item" :alt="item" style="max-height: 30px;max-width: 80px"/>
            </el-popover>
          </span>
        </template>
      </el-table-column>
      <el-table-column
        prop="commentVideos"
        header-align="center"
        align="center"
        label="评论视频">
        <template slot-scope="scope">
          <span v-if="scope.row.commentVideos !== '' && scope.row.commentVideos !== null">
            <el-popover
            style="margin-left: 5px;margin-bottom: 5px"
            placement="right"
            title=""
            trigger="hover" v-for="(item, index) in scope.row.commentVideos.split(',')" :key="index">
                <video v-if="item !== null" :src="'https://image.yilianbaihui.cn/consumer/' +item" style="max-width: 300px"/>
                <video slot="reference" v-if="item !== null" :src="'https://image.yilianbaihui.cn/consumer/' + item" :alt="item" style="max-height: 30px;max-width: 80px"/>
            </el-popover>
          </span>
        </template>
      </el-table-column>
      <el-table-column
        prop="goodsDetail"
        header-align="center"
        align="center"
        label="商品规格">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="deleteHandle(scope.row.commentId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
  </el-dialog>
</template>

<script>
//   import UEditor from '@/components/ueditor/ueditor.vue'
  export default {
    components: {
    //   UEditor
    },
    data () {
      return {
        visible: false,
        dialogImageUrl: '',
        dialogVisible: false,
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        dataList: [],
        id: 0
      }
    },
    methods: {
      init (id) {
        debugger
        this.id = id || 0
        this.visible = true
        this.isDisabled = false
        this.addOrUpdateVisible = false
        this.dataListLoading = true
        this.$nextTick(() => {
          if (this.id) {
            this.getDataList()
          }
        })
      },
      getDataList () {
        this.$http({
          url: this.$http.adornUrl(`/goods/getGoodsCommentById`),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'keywords': this.id,
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.data.list
            this.totalPage = data.data.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.commentId
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/goods/removeGoodsCommentByIds'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
